package leetcode.day_2021_04_12_2021_04_18;

import leetcode.ListNode;

/**
 * 86. 分隔链表
 * https://leetcode-cn.com/problems/partition-list/
 */
public class LeetCode0086 {
    public ListNode partition(ListNode head, int x) {
        ListNode smallHead = new ListNode(0);
        ListNode smallTail = smallHead;
        ListNode maxHead = new ListNode(0);
        ListNode maxTail = maxHead;
        while (head != null) {
            if (head.val < x) {
                smallTail = smallTail.next = head;
            } else {
                maxTail = maxTail.next = head;
            }
            head = head.next;
        }
        smallTail.next = maxHead.next;
        maxTail.next = null;
        return smallHead.next;
    }
}
